<template>
  <div style="margin-top: 14px">
    <el-row :gutter="10" type="flex" justify="end">
      <el-col v-if="showList.includes('configServ') && isConfig" :span="1.5">
        <el-button class="pms-button-primary" @click="openConfigServ(true)">乙供物资明细配置</el-button>
      </el-col>
      <el-col v-if="showList.includes('configServForm') && isConfig" :span="1.5">
        <el-button class="pms-button-primary" @click="openConfigServ()">乙供物资明细查看</el-button>
      </el-col>
      <el-col v-if="showList.includes('pushServMateria')" :span="1.5">
        <el-button class="pms-button-primary" @click="materialSynchronous">物资信息同步资源</el-button>
      </el-col>
      <el-col v-if="showList.includes('resourceEntry')" :span="1.5">
        <el-button class="pms-button-primary" @click="resourceEntry">资源录入</el-button>
      </el-col>
      <el-col v-if="showList.includes('resourceConfirm')" :span="1.5">
        <el-button class="pms-button-primary" @click="resourceConfirm">资源确认</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button class="pms-button-primary" @click="gotoPanorama">VR全景查看</el-button>-->
<!--      </el-col>-->
    </el-row>
    <!-- 服务明细编制弹框 -->
    <ServiceTabs ref="serviceTabsDialog" />
  </div>
</template>

<script>
import ServiceTabs from '@/views/acceptanceManagement/servicecodeDetailConfig/ServiceTabs'
import {
  checkPushResourceAndFinance,
  pushAllResourceAndFinance,
  checkResourceEnter,
  getResourceConfirm,
  getPanoramaUrl
} from '@/api/acceptanceManagement/acceptanceMange'
import {
  getGoToMultiplApi,
} from '@/api/acceptance/index'
import { newOpenPostWindow } from '@/utils/newRequestCommonFormjs'
import { openPostWindow } from '@/utils/requestCommonForm'
import {
  configButtonIsDisplayedApi,
  checkConfigDetailVerifyApi,
  checkServiceDetailsConfigurationApi,
  getPrjDetailApi
} from '@/api/acceptanceManagement/acceptanceMange'
const options = {
  entering: ['configServ', 'pushServMateria', 'resourceEntry'],
  audit: ['configServForm', 'resourceConfirm'],
  look: ['configServForm']
}
export default {
  name: 'YSCommonButton',
  components: { ServiceTabs },
  props: {
    buttonType: {
      type: String,
      required: true
    },
    prjId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      showList: [],
      tabData: {

      },
      isConfig: false
    }
  },
  watch: {
    buttonType() {
      this.initButtonContorl()
    }
  },
  created() {
    this.initButtonContorl()
    this.getConfigButton()
    this.getDetail()
  },
  methods: {
    getDetail() {
      getPrjDetailApi(this.prjId).then(({ data }) => {
        this.tabData = data
      })
    },
    getConfigButton() {
      const params = {
        ifSingleProject: '0',
        subPrjId: '',
        prjId: this.prjId
      }
      configButtonIsDisplayedApi(params).then(res => {
        this.isConfig = res.data
      })
    },
    async openConfigServ(isCheck) {
      if (isCheck) {
        const params = {
          ifSingleProject: '0',
          subPrjId: '',
          prjId: this.prjId
        }
        const res2 = await checkServiceDetailsConfigurationApi(params)
        if (+res2.code !== 200) {
          return
        }
        const res = await checkConfigDetailVerifyApi(params)
        if (+res.code !== 200) {
          return
        }
        if (!res.data) {
          return this.$message.warning('该项目下没有需要配置明细的服务编码，不需要配置服务明细！')
        }
      }
      const tabParams = {
        prjId: [0, -1].includes(this.tabData.PARENTPRJID) ? this.prjId : this.tabData.PARENTPRJID,
        subPrjId: this.prjId,
        ifEdit: isCheck ? '1' : '0',
        ifSingleProject: [0, -1].includes(this.tabData.PARENTPRJID) ? 0 : 1
      }
      this.$refs.serviceTabsDialog.shoService(tabParams)
    },
    // 初始化按钮控制
    initButtonContorl() {
      this.showList = options[this.buttonType]
    },

    // 物资服务同步财务/资源
    async materialSynchronous() {
      const res1 = await checkPushResourceAndFinance(this.prjId)
      if (res1.code === '200') {
        const res2 = await pushAllResourceAndFinance(this.prjId)
        if (res2.code === '200') {
          this.$message.success('推送成功')
        }
      }
    },

    // 资源录入
    async resourceEntry() {
      const res = await checkResourceEnter(this.prjId)
      if (res.code === '200') {
        const params = {}
        params.pwdaToken = res.data.pwdaToken
        params.acctId = res.data.acctId
        openPostWindow(res.data.jumpUrl, params, res.data.isNew)
      }
    },

    // 资源确认
    async resourceConfirm() {
      // 30008283 ---新项目
      // 138690 =
      const _params = {
        subPrjIdList: [this.prjId],
      }
      const res = await getGoToMultiplApi('',_params.subPrjIdList)
      if (res.code === '200') {
        const params = {}
        params.pwdaToken = res.data.pwdaToken
        params.acctId = res.data.acctId
        newOpenPostWindow(res.data.jumpUrl, {}, res.data.isNew)
      }
    },
    // 跳转至资源系统-VR全景查看
    async gotoPanorama() {
      const res = await getPanoramaUrl(this.prjId)
      console.log('res', res)
      if (res.code === '200') {
        window.open(res.data.jumpUrl)
      }
    }
  }
}

</script>
